import axios from 'axios'
import { message } from 'antd'


const baseURL = '/flowable-ui/'

const request = axios.create({
    baseURL: baseURL,
    timeout: 10 * 1000
})

request.interceptors.request.use(config => {
    return config;
}, (error) => {
    return Promise.reject(error);
})

request.interceptors.response.use(
    response => {
        const requestUrl = response.config.url
        const responseUrl = response.request.responseURL.split(baseURL)[1].split('?')[0]
        if (requestUrl === responseUrl) {
            return response.data || {}
        } else {
            if (!response.config?.noMessage) {
                message.error('请求失败')
            }
            return Promise.reject('请求失败')
        }
    },
    error => {
        const msg = error?.response?.data?.message || error?.response?.data?.error
        if (!error.config.noMessage) {
            message.error(msg || '请求失败')
        }
        return Promise.reject(error?.response?.data || error)
    }
)

export default request